//
//  UIButton-Extension.swift
//  Stauts
//
//  Created by Kylin on 16/3/13.
//  Copyright © 2016年 Kylin. All rights reserved.
//

import UIKit

// MARK:- UIButton的分类方法
extension UIButton {
    // swift中如果在extension中扩展构造函数,必须在构造函数前convenience(便利)
    // 便利构造函数中,必须明确调用self.init()
    
    convenience init (imageName : String,bgimageName : String){
        self.init()
        
        setImage(UIImage(named: imageName), forState: .Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        setBackgroundImage(UIImage(named: bgimageName), forState: .Normal)
        setBackgroundImage(UIImage(named: bgimageName + "_highlighted"), forState: .Highlighted)
        sizeToFit()
    }
    
    convenience init(title : String ,BGcolor : UIColor,fontSize : CGFloat){
        self.init()
        setTitle(title, forState: .Normal)
        backgroundColor = BGcolor
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
    }
    // 在swift中类方法只需要以class开头即可:类似于OC中+开头的方法
    class func createButton (imageName : String,bgimageName : String) -> UIButton {
        // 创建button
        let button = UIButton(type: .Custom)
        
        // 设置图片
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: bgimageName), forState: .Normal)
        button.setBackgroundImage(UIImage(named: bgimageName + "_highlighted"), forState: .Highlighted)
        button.sizeToFit()
        
        return button
    }
}


// MARK:- 首页导航栏按钮的自定义按钮重写的方法

class titleButton: UIButton {
     // swift中,如果是实现了父类的某一个构造函数.那么如果父类有required修饰的构造函数,那么子类必须实现
    override init(frame: CGRect) {
        super.init(frame: frame)
        setImage(UIImage(named: "navigationbar_arrow_down"), forState: .Normal)
        setImage(UIImage(named: "navigationbar_arrow_up"), forState: .Selected)
        setTitleColor(UIColor.blackColor(), forState: .Normal)
        sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        titleLabel?.frame.origin.x = 0
        imageView?.frame.origin.x = titleLabel!.frame.width + 5
    }
    
}